package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;

import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 大佬味的小男孩
 * @date: 2020-07-27 21:56
 **/
@Service
public class PageServiceImpl implements PageService {

    @Value("${pagepath}")
    private String pagepath;

    @Autowired
    private TemplateEngine templateEngine;

    /**
     * @param spuId
     * @return void
     * @description: 生产静态化页面
     * @author 大佬味的小男孩
     * @date 2020/07/27 21:57
     */
    @Override
    public void generateHtml(String spuId) {

        //获取context对象
        Context context = new Context();
        //获取静态化页面的数据
        Map<String, Object> map = this.getItemData(spuId);
        context.setVariables(map);


        //获取商品存储位置(本地磁盘)
        File file = new File(pagepath);
        if (!file.exists()) {
            //判断存储静态化页面的文件夹是否存在  如果不存在 就创建
            file.mkdir();
        }
        //定义输出流  生产文件
        File file1 = new File(file + "/" + spuId + ".html");
        Writer out = null;
        try {
            out = new PrintWriter(file1);
            templateEngine.process("item", context, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭流
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private SkuFeign skuFeign;

    /**
     * @param spuId
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @description: //获取静态化页面的数据
     * @author 大佬味的小男孩
     * @date 2020/07/27 22:01
     */
    private Map<String, Object> getItemData(String spuId) {

        HashMap<String, Object> map = new HashMap<>();
        //获取spu数据
        Spu spu = spuFeign.findSpuById(spuId).getData();

        map.put("spu", spu);

        //获取图片数据
        if (spu != null) {
            if (!StringUtils.isEmpty(spu.getImages())) {
                map.put("ImageList", spu.getImages().split(","));
            }
        }

        //获取商品分类数据
        Category category1 = categoryFeign.findById(spu.getCategory1Id());
        Category category2 = categoryFeign.findById(spu.getCategory2Id());
        Category category3 = categoryFeign.findById(spu.getCategory3Id());
        map.put("category1", category1);
        map.put("category2", category2);
        map.put("category3", category3);

        //获取sku的数据
        List<Sku> skuList = skuFeign.findSkuListByspuid(spuId);
        map.put("skuList", skuList);

        //获取规格信息
        map.put("specifcationList", JSON.parseObject(spu.getSpecItems(), Map.class));

        return map;
    }
}
